﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Argument.Is
{
    /// <summary>
    /// Represents a contract that adheres to <b>Argument is an instance of</b> semantic.
    /// </summary>
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Method)]
    [ProvideAspectRole(Roles.Argument)]
    [ProvideAspectRole(Roles.Conditional)]
    [Serializable]
    public sealed class InstanceOfAttribute : ContractBase
    {
        private Type _type;

        /// <summary>
        /// Initializes a new instance of <b>Argument.Is.InstanceOfAttribute</b>.
        /// </summary>
        /// <param name="argumentName">The argument to which this contract applies.</param>
        /// <param name="type">The type which <i>argument</i> should be a type of.</param>
        /// <exception cref="ArgumentNullException"><i>argumentName</i> or <i>type</i> is <b>null</b>.</exception>
        public InstanceOfAttribute(string argumentName, Type type)
            : base(argumentName)
        {
            if (type == null) throw new ArgumentNullException();
            _type = type;
        }

        /// <summary>
        /// Performs validation on the argument.
        /// </summary>
        /// <param name="argument">The argument that should be validated.</param>
        /// <returns><b>true</b> if <i>argument</i> is validated, otherwise <b>false</b>.</returns>
        protected override bool ValidateArgument(object argument)
        {
            if (argument == null) return true;
            return argument.GetType().Equals(_type);
        }
    }
}
